package baidu.com.matedemo.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.duer.libcore.view.ProgressDialog;

import baidu.com.matedemo.DemoConstant;
import baidu.com.matedemo.R;

/**
 * Created by niufei01 on 2017/12/29.
 */

public class BaseActivity extends FragmentActivity {

    private Fragment fragment;
    private ImageView TitleBar1Left;
    private TextView TitleBar1Title;
    private LinearLayout contentLayout;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_base);
        initView();

        View view = onCreateContentViewCompat(LayoutInflater.from(this), contentLayout, savedInstanceState);
        contentLayout.addView(view);
        onContentViewCreated(view);
    }

    private void initView() {
        TitleBar1Left = (ImageView) findViewById(R.id.TitleBar1_Left);
        TitleBar1Title = (TextView) findViewById(R.id.TitleBar1_Title);
        contentLayout = (LinearLayout) findViewById(R.id.content_layout);
        TitleBar1Left.setVisibility(View.VISIBLE);
        TitleBar1Left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public void setTitle(String title) {
        TitleBar1Title.setText(title);
    }

    public void onContentViewCreated(View view) {
        String title = getIntent().getStringExtra(DemoConstant.EXTRA_TITLE);
        if (!TextUtils.isEmpty(title)) {
            TitleBar1Title.setText(title);
        }
    }

    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.layout_base_content, viewGroup, false);
        if (fragment == null) {
            String className = getIntent().getStringExtra(DemoConstant.EXTRA_FRAGMENT_TYPE);
            if (className != null) {
                fragment = getFragment(className);
                FragmentTransaction trans = this.getSupportFragmentManager().beginTransaction();
                trans.replace(view.getId(), fragment);
                trans.commitAllowingStateLoss();
                trans.show(fragment);

            }
        }
        return view;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle newBundle = intent.getExtras();
        if (fragment != null) {
            fragment.setArguments(newBundle);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    /***
     * 创建Fragment
     * @param className
     * @return
     */
    protected Fragment getFragment(String className) {
        Bundle args = getIntent().getExtras();
        Fragment fragment = Fragment.instantiate(this, className, args);
        return fragment;
    }

    public void showProgressBar() {

        progressDialog = new ProgressDialog(this);
        progressDialog.show();
    }

    public void dismissProgressBar() {
        try {
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
        } catch (Exception e) {
            // do nothing
        }
    }

}
